<?php

namespace App\Kuafu\User\Validate;

use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\User\Enum\UserCodeEnum;
use App\Kuafu\User\Model\User;
use App\Kuafu\User\Model\UserRoleMapping;
use Illuminate\Http\Request;

class UserValidate extends BaseValidate
{

    public static function register(Request $request)
    {
        $rules    = [
            'password' => 'required',
            'username' => [
                'required',
                'max:20',
                // function ($attribute, $value, $fail) use ($request) {
                //     if (User::query()->where(['username' => $request->username])->first()) {
                //         $fail("该用户名已被占用");
                //     }
                // }
            ],
            'mobile'   => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (User::query()->where(['mobile' => $request->mobile])->first()) {
                        $fail("该手机号已被注册");
                    }
                }
            ]
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function loginValid(Request $request)
    {
        $rules    = [
            'username' => 'required',
            'password' => 'required',
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }


    public static function addUser(Request $request)
    {
        $rules    = [
            'username'     => [
                'required',
                'max:20',
                // function ($attribute, $value, $fail) use ($request) {
                //     if (User::query()->where(['username' => $request->username])->withoutGlobalScope('isDel')
                //         ->first()) {
                //         $fail("该用户名已被占用");
                //     }
                // }
            ],
            'mobile'       => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (User::query()->where(['mobile' => $request->mobile])->withoutGlobalScope('isDel')->first()) {
                        $fail("该手机号已被注册");
                    }
                }
            ],
            'password'     => [
                'required',
                function ($attribute, $value, $fail) use ($request) {
                    if ($request->password != $request->rePassword) {
                        $fail("两次密码输入不一致");
                    }
                }
            ],
            'rePassword'   =>
                [
                    'required',
                    function ($attribute, $value, $fail) use ($request) {
                        if ($request->password != $request->rePassword) {
                            $fail("两次密码输入不一致");
                        }
                    }
                ],
            'roleIdList.*' => 'integer'

        ];
        $messages = [
            'roleIdList.*.integer' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_ROLE],
        ];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function setUserInfo(Request $request)
    {
        $rules    = [
            'userId'   => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (!User::query()->where(['id' => $request->userId])->first()) {
                        $fail("用户不存在");
                    }
                }
            ],
            'username' => [
                'required',
                'max:20',
                // function ($attribute, $value, $fail) use ($request) {
                //     if (User::query()->where(['username' => $request->username])->where('id', '!=', $request->userId)
                //         ->first()) {
                //         $fail("该用户名已被占用");
                //     }
                // }
            ],
            'mobile'   => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (User::query()->where(['mobile' => $request->mobile])->where('id', '!=', $request->userId)
                        ->first()) {
                        $fail("该手机号已被注册");
                    }
                }
            ]
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function setUserStatus(Request $request)
    {
        $rules    = [
            'userId' => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (!User::query()->where(['id' => $request->userId])->first()) {
                        $fail("用户不存在");
                    }
                }
            ],
            'status' => [
                'required',
                'integer',
                'between:0,1'
            ]
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }


    public static function setUserPassword(Request $request): string
    {
        $rules    = [
            'userId'   => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (!User::query()->where(['id' => $request->userId])->first()) {
                        $fail("用户不存在");
                    }
                }
            ],
            'password' => ['required']
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function checkUserIsExist(Request $request): string
    {
        $rules          = [
            'userId' => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    if (!User::query()->where(['id' => $request->userId])->first()) {
                        $fail("用户不存在");
                    }
                }
            ],
        ];
        $data['userId'] = $request->userId;
        $messages       = [];
        return self::validateCheck($data, $rules, $messages);
    }

    //新增员工信息
    public static function addStaffInfo(Request $request)
    {
        $rules    = [
            'username'   => [
                'required',
                'max:20',
                // function ($attribute, $value, $fail) use ($request) {
                //     if (User::query()->where(['username' => $request->username])->first() && User::query()->where(['username' => $request->username])->first()->mobile != $request->mobile) {
                //         $fail("该用户名已被占用");
                //     }
                // }
            ],
            'mobile'     => [
                'required',
                'integer',
                // function ($attribute, $value, $fail) use ($request) {
                //     $userIds = UserRoleMapping::query()->where('shopId', $request->header('shopId'))->pluck('userId')->toArray();
                //     $mobiles = User::query()->whereIn('id', $userIds)->pluck('mobile')->toArray();
                //     if (!in_array($request->mobile, $mobiles)) {
                //         return true;
                //     } else {
                //         $fail("该手机号已被注册");
                //     }
                // }
            ],
            'inShop'     => [
                'integer',
                'between:0,1'
            ],
            'positions'  => [
                'required'
            ],
            // 'induction'  => [
            //     'required'
            // ],
            'idCardFacePhoto'     => [
                'string',
            ],
            'idCardBackPhoto'     => [
                'string',
            ],
            'healthPhoto'         => [
                'string',
            ],
            'healthPhotoDate'     => [
                'string',
            ],
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function setStaffInfo(Request $request)
    {
        $rules    = [
            'username'   => [
                'max:20',
                'required',
                // function ($attribute, $value, $fail) use ($request) {
                //     $user = User::query()->where(['username' => $request->username])->first();
                //     if ($user == null) {
                //         return true;
                //     } else {
                //         if ($user->id != $request->userId) {
                //             $fail("该用户名已被占用");
                //         }
                //     }
                // }
            ],
            'mobile'     => [
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    $userIds = UserRoleMapping::query()->where('shopId', $request->header('shopId'))->pluck('userId')->toArray();
                    $mobiles = User::query()->whereIn('id', $userIds)->pluck('mobile', 'id')->toArray();
                    if (!in_array($request->mobile, $mobiles)) {
                        return true;
                    } else {
                        if ($mobiles[$request->userId] != $request->mobile) {
                            $fail("该手机号已被注册");
                        }
                    }
                }
            ],
            'inShop'     => [
                'between:0,1'
            ],
            'userId'     => [
                'integer',
                'required'
            ],
            'idCardFacePhoto'     => [
                'string',
            ],
            'idCardBackPhoto'     => [
                'string',
            ],
            'healthPhoto'         => [
                'string',
            ],
            'healthPhotoDate'     => [
                'string',
            ],
            // 'induction'  => [
            //     'required'
            // ]
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function bindingId(Request $request)
    {
        $rules    = [
            'registrationId' => [
                'required'
            ],
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function getShopIds(Request $request)
    {
        $rules    = [
            'shopIds' => [
                'required',
                function ($attribute, $value, $fail) use ($request) {
                    if (!is_array($request->post('shopIds'))) {
                        $fail("参数类型不正确");
                    }
                }
            ],
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function createInternal(Request $request)
    {
        $rules    = [
            'name'    => 'required|string',
            'mobile'  => 'required|string',
            'roles'   => 'array',
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }

    //新增员工信息-已有手机号时
    public static function addStaffInfoByMobileExist(Request $request): string
    {
        $rules    = [
            'username'  => [
                'required',
                'max:20',
                // function ($attribute, $value, $fail) use ($request) {
                //     if (User::query()->where(['username' => $request->username])->first() && User::query()
                //                                                                                  ->where(['username' => $request->username])
                //                                                                                  ->first()->mobile != $request->mobile) {
                //         $fail("该用户名已被占用");
                //     }
                // }
            ],
            'mobile'    => [
                'required',
                'integer',
                function ($attribute, $value, $fail) use ($request) {
                    $userIds = UserRoleMapping::query()->where('shopId', $request->header('shopId'))->pluck('userId')
                                              ->toArray();
                    $mobiles = User::query()->whereIn('id', $userIds)->pluck('mobile')->toArray();
                    if (!in_array($request->mobile, $mobiles)) {
                        return true;
                    } else {
                        $fail("门店员工该已绑定该手机号，请更换手机号添加");
                    }
                }
            ],
            'positions' => [
                'required'
            ],
            'inShop'     => [
                'between:0,1'
            ],
            'idCardFacePhoto'     => [
                'string',
            ],
            'idCardBackPhoto'     => [
                'string',
            ],
            'healthPhoto'         => [
                'string',
            ],
            'healthPhotoDate'     => [
                'string',
            ],
            // 'induction'  => [
            //     'required'
            // ]
        ];
        $messages = [];
        return self::validateCheck($request->all(), $rules, $messages);
    }
}
